//
//  ZFFinanceInfoViewController.m
//  HappilyLoan
//
//  Created by zainishenbian on 2017/3/28.
//  Copyright © 2017年 zongqingxiangqian. All rights reserved.
//

#import "ZFFinanceInfoViewController.h"
#import "ZFHeader.h"

@interface ZFFinanceInfoViewController ()
{
    NSString *startTime;
}
@property (nonatomic,strong) AnimatingView *animation;
@property (nonatomic,strong) AlertBoxView *alertV;
@end

@implementation ZFFinanceInfoViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"financeInfo"];
    self.navigationController.navigationBarHidden = NO;
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"financeInfo"];
}
- (void)submitStatu:(ReturnStatuBlock)block
{
    self.submitStatuBlock = block;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"财务信息";
    
    if (_financeModal) {
        
        [self showData];
    }

}
- (void)initData
{
    _animation = [[AnimatingView alloc] init];
    
    _alertV = [[AlertBoxView alloc] init];
    [self.view addSubview:_alertV];
}
#pragma mark  ———— show data
- (void)showData
{
    _payTF.text = _financeModal.salary_income;
    
    if (_financeModal.other_income.length > 0) {
        
        _otherTF.text = _financeModal.other_income;
    }
    if (_financeModal.owe_loan.length > 0) {
        
        _loanTF.text = _financeModal.owe_loan;
    }
}
#pragma mark ———— textfield delegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSMutableString * futureString = [NSMutableString stringWithString:textField.text];
    
    [futureString  insertString:string atIndex:range.location];
    
    NSInteger flag=0;
    
    const NSInteger limited = 2;//小数点后需要限制的个数
    
    for (int i = (int)futureString.length-1; i>=0; i--) {
        
        if ([futureString characterAtIndex:i] == '.') {
            if (flag > limited) {
                
                return NO;
            }
            break;
        }
        flag++;
    }
    return YES;
}

#pragma mark ———— button的点击事件
//提交信息
- (IBAction)sumbitFinance:(id)sender {
    
    if ([ZFPulicAPI replaceBlank:_payTF.text].length > 0) {
        
        ZFFinancelInfoModal *financeInfo = [ZFFinancelInfoModal new];
        financeInfo.salary_income = [ZFPulicAPI replaceBlank:_payTF.text];
        financeInfo.startTime = startTime;
        if ([ZFPulicAPI replaceBlank:_otherTF.text].length > 0) {
            
            financeInfo.other_income = [ZFPulicAPI replaceBlank:_otherTF.text];
        }
        else
        {
            financeInfo.other_income = @"";
        }
        if ([ZFPulicAPI replaceBlank:_loanTF.text].length > 0) {
            
            financeInfo.owe_loan = [ZFPulicAPI replaceBlank:_loanTF.text];
        }
        else
        {
            financeInfo.owe_loan = @"";
        }
        
        [_animation startTheAnimation];
        [ZFApplyInfoHttpRequest requestSubmitFinancelInfo:financeInfo sucessBlock:^(NSDictionary *dict) {
            
            [_animation stopTheAnimation];
            if ([dict[@"is_alert"] integerValue] == 1) {
                
                [_alertV appearAndDisappearAlertWithString:dict[@"msg"]];
            }
            if ([dict[@"code"] integerValue] == 1) {
                
                self.submitStatuBlock(1);
                [self.navigationController popViewControllerAnimated:YES];
            }
            else
            {
                self.submitStatuBlock(0);
            }

        } failureBlock:^(NSInteger failSign) {
            
            [_animation stopTheAnimation];
            [_alertV appearAndDisappearAlertWithString:@"网络连接失败"];
        }];
    }
    else
    {
        [_alertV appearAndDisappearAlertWithString:@"信息不完善" ];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
